#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
查看数据库数据统计
"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from app import create_app
from models import db, User, Class, Problem, Assignment, AssignmentSubmission, SystemLog
from datetime import datetime

def check_data():
    """查看数据统计"""
    app = create_app()
    with app.app_context():
        print("=" * 50)
        print("数据库数据统计")
        print("=" * 50)
        
        # 用户统计
        total_users = User.query.count()
        admin_users = User.query.filter_by(role='admin').count()
        teacher_users = User.query.filter_by(role='teacher').count()
        student_users = User.query.filter_by(role='student').count()
        
        print(f"\n👥 用户统计:")
        print(f"  总用户数: {total_users}")
        print(f"  管理员: {admin_users}")
        print(f"  教师: {teacher_users}")
        print(f"  学生: {student_users}")
        
        # 班级统计
        total_classes = Class.query.count()
        active_classes = Class.query.filter_by(status='active').count()
        
        print(f"\n🏫 班级统计:")
        print(f"  总班级数: {total_classes}")
        print(f"  活跃班级: {active_classes}")
        
        # 题目统计
        total_problems = Problem.query.count()
        easy_problems = Problem.query.filter_by(difficulty='easy').count()
        medium_problems = Problem.query.filter_by(difficulty='medium').count()
        hard_problems = Problem.query.filter_by(difficulty='hard').count()
        
        print(f"\n📝 题目统计:")
        print(f"  总题目数: {total_problems}")
        print(f"  简单题目: {easy_problems}")
        print(f"  中等题目: {medium_problems}")
        print(f"  困难题目: {hard_problems}")
        
        # 作业统计
        total_assignments = Assignment.query.count()
        active_assignments = Assignment.query.filter_by(status='active').count()
        
        print(f"\n📚 作业统计:")
        print(f"  总作业数: {total_assignments}")
        print(f"  活跃作业: {active_assignments}")
        
        # 提交统计
        total_submissions = AssignmentSubmission.query.count()
        accepted_submissions = AssignmentSubmission.query.filter_by(status='accepted').count()
        pending_submissions = AssignmentSubmission.query.filter_by(status='pending').count()
        
        print(f"\n📤 提交统计:")
        print(f"  总提交数: {total_submissions}")
        print(f"  通过提交: {accepted_submissions}")
        print(f"  待处理提交: {pending_submissions}")
        
        # 系统日志统计
        total_logs = SystemLog.query.count()
        info_logs = SystemLog.query.filter_by(type='info').count()
        success_logs = SystemLog.query.filter_by(type='success').count()
        warning_logs = SystemLog.query.filter_by(type='warning').count()
        error_logs = SystemLog.query.filter_by(type='error').count()
        
        print(f"\n📊 系统日志统计:")
        print(f"  总日志数: {total_logs}")
        print(f"  信息日志: {info_logs}")
        print(f"  成功日志: {success_logs}")
        print(f"  警告日志: {warning_logs}")
        print(f"  错误日志: {error_logs}")
        
        # 详细班级信息
        print(f"\n🏫 班级详细信息:")
        classes = Class.query.all()
        for i, class_obj in enumerate(classes, 1):
            print(f"  {i}. {class_obj.name}")
            print(f"     教师: {class_obj.teacher.name}")
            print(f"     学生数: {len(class_obj.students)}/{class_obj.max_students}")
            print(f"     状态: {class_obj.status}")
            print(f"     描述: {class_obj.description[:50]}...")
        
        # 详细作业信息
        print(f"\n📚 作业详细信息:")
        assignments = Assignment.query.all()
        for i, assignment in enumerate(assignments, 1):
            problem_ids = json.loads(assignment.problem_ids) if assignment.problem_ids else []
            print(f"  {i}. {assignment.title}")
            print(f"     班级: {assignment.class_obj.name}")
            print(f"     题目数: {len(problem_ids)}")
            print(f"     截止时间: {assignment.due_date}")
            print(f"     总分: {assignment.total_score}")
            print(f"     状态: {assignment.status}")
        
        # 用户登录信息
        print(f"\n👤 用户详细信息:")
        users = User.query.all()
        for user in users:
            print(f"  {user.username} ({user.name}) - {user.role}")
            if user.role == 'teacher':
                managed_classes = len(user.managed_classes)
                print(f"    管理班级数: {managed_classes}")
            elif user.role == 'student':
                enrolled_classes = len(user.enrolled_classes)
                print(f"    加入班级数: {enrolled_classes}")
        
        print(f"\n" + "=" * 50)
        print("数据统计完成！")
        print("=" * 50)

if __name__ == '__main__':
    import json
    check_data()
